﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entity
{
    public class SMDBContext : DbContext
    {
        private readonly string? _ConnectionString;

        public SMDBContext(DbContextOptions options) : base(options)
        {
        }

        public SMDBContext(DbContextOptions<SMDBContext> opts, IConfiguration configuration) : base(opts)
        {
            _ConnectionString = configuration.GetConnectionString("AIYDB_MySql");
        }

        /// <summary>
        /// 账户
        /// </summary>
        public DbSet<EntityAccount> EntityAccount { get; set; }

        /// <summary>
        /// 用户信息
        /// </summary>
        public DbSet<EntityUserInFo> EntityUserInFo { get; set; }

        /// <summary>
        /// 报名表
        /// </summary>
        public DbSet<EntityEntryForm> EntityEntryForm { get; set; }



        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
        //    optionsBuilder.UseMySql(_ConnectionString, new MySqlServerVersion(new Version(9, 2, 0)));


        //}
        //protected override void OnModelCreating(ModelBuilder modelBuilder)
        //{
        //    modelBuilder
        //        .UseCollation("utf8mb4_german2_ci")
        //        .HasCharSet("utf8mb4");

        //}

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            EntityUserInFo user = new EntityUserInFo();
            user.UserName = "123456";
            user.ListName = "3453";

            modelBuilder.Entity<EntityUserInFo>().HasData([
                new EntityUserInFo{
                    UserName="zhangsan",
                    ListName="san",
                    Email="4444sdfsdf@sdf.cvf"
                }

                ]);


        }

        public class SMDBContextFactory : IDbContextFactory<SMDBContext>
        {

            public SMDBContext CreateDbContext()
            {
                var optionBuilder = new DbContextOptionsBuilder<SMDBContext>();
                optionBuilder.UseMySql(
                    "server=localhost;userid=root;password=123456;database=AIYDB;port=3306;",
                    new MySqlServerVersion(new Version(8, 0, 41)));
                return new SMDBContext(optionBuilder.Options);
            }
        }
    }
}
